home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / scrasm / palette.inc < prev    next >
Encoding:
Text File  |  1993-03-08  |  9.1 KB  |  239 lines

  1. ;; Palette operations
  2. ;; Note that where needed in the macros, a "palette" refers to
  3. ;; the segment handle to a 768-byte piece of memory.  So palettes
  4. ;; can be loaded and freed, they're not permanent, but if you want
  5. ;; to use a fixed (not allocated) palette you'd better make sure
  6. ;; it's segment aligned or else you can't use these macros.  If it
  7. ;; is, you can just supply "seg myPalette" as the 'palette' argument
  8. ;; to any of these macros.
  9.  
  10. ;; Fade from a palette to black
  11. FADE_OFF        MACRO   fade,palette
  12.                 mov     si,0
  13.                 mov     ds,palette
  14.                 mov     bh,fade         ; positive -> Gets dimmer...
  15.                 mov     bl,0            ; Starts exact
  16.                 mov     cx,64/fade+1    ; Total number of loops required
  17.                 call    FadePalette
  18.                 ENDM
  19.  
  20. ;; Fade from black to a palette
  21. FADE_ON         MACRO   fade,palette
  22.                 mov     si,0
  23.                 mov     ds,palette
  24.                 mov     bh,-fade        ; negative -> Gets brighter...
  25.                 mov     bl,64           ; Starts totally dimmed
  26.                 mov     cx,64/fade+1    ; Total number of loops required
  27.                 call    FadePalette
  28.                 ENDM
  29.  
  30. ;; Flash from a palette to white
  31. FLASH_OFF       MACRO   fade,palette
  32.                 mov     si,0
  33.                 mov     ds,palette
  34.                 mov     bh,-fade        ; negative -> gets brighter
  35.                 mov     bl,0            ; Starts exact
  36.                 mov     cx,64/fade+1    ; Total number of loops required
  37.                 call    FadePalette
  38.                 ENDM
  39.  
  40. ;; Flash from white to a palette
  41. FLASH_ON        MACRO   fade,palette
  42.                 mov     si,0
  43.                 mov     ds,palette
  44.                 mov     bh,fade         ; positive -> Gets dimmer...
  45.                 mov     bl,-64          ; Starts totally bright
  46.                 mov     cx,64/fade+1    ; Total number of loops required
  47.                 call    FadePalette
  48.                 ENDM
  49.  
  50. ;; Save a palette into a palette-sized piece of memory
  51. PAL_SAVE        MACRO   palette
  52.                 mov     es,palette
  53.                 mov     di,0
  54.                 call    SavePalette
  55.                 ENDM
  56.  
  57. ; Returns AX = a new segment for a palette
  58. NEW_PAL         MACRO   palette
  59.                 mov     bx,(256 * 3) / 16
  60.                 mov     ah,48h
  61.                 int     21h
  62.                 mov     palette,ax
  63.                 ENDM
  64.  
  65. ;; Black the entire palette temporarily.  Used to blank the screen while
  66. ;; drawing a frame before fading in.
  67. PAL_BLACK       MACRO
  68.                 mov     ax,seg tmppal
  69.                 mov     ds,ax
  70.                 mov     si,OFFSET tmppal
  71.                 mov     bh,-1           ; Doesn't really matter...
  72.                 mov     bl,64           ; Starts totally dimmed
  73.                 mov     cx,1            ; Just one time -- to leave it black
  74.                 call    FadePalette
  75.                 ENDM
  76.  
  77. ;; drawing a frame before fading in.
  78. PAL_WHITE       MACRO
  79.                 mov     ax,seg tmppal
  80.                 mov     ds,ax
  81.                 mov     si,OFFSET tmppal
  82.                 mov     bh,-1           ; Doesn't really matter...
  83.                 mov     bl,-64          ; Starts totally dimmed
  84.                 mov     cx,1            ; Just one time -- to leave it black
  85.                 call    FadePalette
  86.                 ENDM
  87.  
  88. ;; Black the entire palette temporarily.  Used to blank the screen while
  89. ;; drawing a frame before fading in.
  90. PAL_UPDATE      MACRO
  91.                 mov     cx,0            ; 0 times = update
  92.                 call    FadePalette
  93.                 ENDM
  94.  
  95. WAITBORDER      MACRO
  96.                 LOCAL   wbr1,wbr2
  97.                 mov     dx,INPUT_STATUS_1
  98. wbr1:           in      al,dx
  99.                 test    al,8
  100.                 jnz     wbr1
  101. wbr2:           in      al,dx
  102.                 test    al,8
  103.                 jz      wbr2
  104.                 ENDM
  105.  
  106. ;; Fade Palette:
  107. ;; The following code is modified greatly from the Future Crew's palette
  108. ;; fading code.  Works on blocks of 256 colors only, so far, but I might
  109. ;; change it later.  Also, it theoretically could "anti-fade" -- fade to
  110. ;; white -- which I call flashing, so I added that ability, which was
  111. ;; missing from FC's code.
  112. EVEN
  113. tmppal          DB      768 dup (?)      ; Stores old palette
  114. FadePalette     PROC NEAR
  115.                 mov     ax,seg tmppal
  116.                 mov     es,ax
  117.  
  118. FadeLoop:       push    cx
  119.                 push    si
  120.  
  121.                 cmp     cx,0
  122.                 je      JustUpdate
  123.  
  124.         ; Load in the colors in the palette
  125.                 mov     di,OFFSET tmppal ; ES:DI -> temp palette
  126.                 mov     cx,768          ; Reads 256*3 bytes at a time.
  127. loadpal_loop:   mov     al,ds:[si]      ; Load one color byte
  128.                 inc     si
  129.                 sub     al,bl           ; Subtract the fade amount
  130.                 jge     pal_more        ; Limit the range by clipping
  131.                 xor     al,al           ;  to between 0 and 63
  132.                 jmp     pal_ok          ; (there's probably a faster
  133. pal_more:       cmp     al,63           ; way to do it than this,
  134.                 jle     pal_ok          ; but I don't know it)
  135.                 mov     al,63
  136. pal_ok:         mov     es:[di],al      ; Store that byte in the new
  137.                 inc     di
  138.                 dec     cx              ;  temp palette and loop.
  139.                 jnz     loadpal_loop
  140.  
  141.         ; Get ready to move this block of palette values
  142. JustUpdate:     sti                     ; Let interrupts happen now,
  143.                 WAITBORDER              ;  while waiting for a retrace,
  144.                 cli                     ;  instead of more critical times
  145.  
  146.                 mov     dx,PEL_WRITE_REG; Set up to write to color register,
  147.                 xor     al,al           ; starting at palette entry 0.
  148.                 out     dx,al
  149.                 mov     dx,PEL_DATA_REG ; Point at color port
  150.  
  151.         ; Quickly put out the first half of the color palette
  152.                 mov     di,OFFSET tmppal
  153.                 mov     cl,(768/6)/2    ; Does 2 loops of 128 colors each.
  154.                 cli                     ;  Waits a retrace inbetween...
  155. FirstHalfLoop:  REPEAT 6                ; Steps of 6 -- reduces the
  156.                 mov     al,es:[di]      ; number of LOOP instructions
  157.                 inc     di
  158.                 out     dx,al
  159.                 ENDM
  160.                 dec     cl
  161.                 jnz     FirstHalfLoop
  162.                 sti
  163.  
  164.                 WAITBORDER              ; Waits one retrace -- less flicker
  165.                 mov     dx,PEL_DATA_REG ; Reset DX
  166.  
  167.         ; Now, quickly put out the other half of the colors.
  168.                 mov     cl,(768/6)/2
  169.                 cli
  170. SecondHalfLoop: REPEAT 6                ; Steps of 6 -- reduces the
  171.                 mov     al,es:[di]      ; number of LOOP instructions
  172.                 inc     di
  173.                 out     dx,al
  174.                 ENDM
  175.                 dec     cl
  176.                 jnz     SecondHalfLoop
  177.  
  178.         ; For the next iteration, restore everything and loop
  179.                 pop     si
  180.                 pop     cx
  181.  
  182.                 cmp     cx,0
  183.                 je      JustUpdated
  184.  
  185.                 add     bl,bh           ; Change brightness by BH
  186.  
  187.                 dec     cx
  188.                 jnz     FadeLoop
  189.  
  190.         ; All done, re-enable interrupts and return
  191. JustUpdated:    sti
  192.                 ret
  193. FadePalette     ENDP
  194.  
  195. ;; Saves the palette into the memory pointed at by DS:SI.  That memory
  196. ;; must be at least 768 bytes long...
  197. SavePalette     PROC NEAR
  198.                 mov     dx,PEL_READ_REG ; Set up to read from color register,
  199.                 xor     al,al           ; starting at palette entry 0.
  200.                 out     dx,al
  201.                 mov     dx,PEL_DATA_REG
  202.  
  203.         ; Quickly read in the first half of the color palette
  204.                 mov     cl,(768/6)
  205.                 cli
  206. ReadPalLoop:    REPEAT 6                ; Steps of 6 -- reduces the
  207.                 in      al,dx           ; number of LOOP instructions
  208.                 mov     es:[di],al
  209.                 inc     di
  210.                 ENDM
  211.                 dec     cl
  212.                 jnz     ReadPalLoop
  213.         ; All done, re-enable interrupts and return
  214.                 sti
  215.                 ret
  216. SavePalette     ENDP
  217.  
  218. ;; Load a palette from a file.  Opens the file and reads it into
  219. ;; memory (standard LoadFile) and then points the palette at that
  220. ;; newly allocated memory.  Also, frees old memory before it does
  221. ;; any loading ...
  222. LoadPaletteFile PROC    near
  223.                 mov     ax,segPalette
  224.                 cmp     ax,-1
  225.                 je      pal_not_loaded
  226.                 mov     es,ax
  227.                 mov     ah,49h
  228.                 int     21h
  229.                 mov     nError,ERR_MEM
  230.                 jc      lp_err
  231.                 mov     segPalette,-1
  232.  
  233. pal_not_loaded: call    LoadFile
  234.                 jc      lp_err
  235.  
  236.                 mov     segPalette,dx
  237. lp_err:         ret
  238. LoadPaletteFile ENDP
  239.